package com.jasonli.controller;

import com.jasonli.domain.Client;
import com.jasonli.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/clients")
public class ClientController {

    @Autowired
    private ClientService clientService;

    @GetMapping("/{username}/{password}")
    public Result getByUsernameAndPassword(@PathVariable String username, @PathVariable String password, HttpSession session) {
        Client newUser = clientService.getByUsernameAndPassword(username, password);
        boolean flag = newUser != null ? true : false;
        return new Result(flag ? Code.LOGIN_OK:Code.LOGIN_ERR,newUser);
    }

    @PostMapping
    public Result save(@RequestBody Client client) {
        boolean flag = clientService.save(client);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    @GetMapping
    public Result getAll() {
        List<Client> infoList = clientService.getAll();
        Integer code = infoList != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code, infoList);
    }

}
